﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace MP3Ganizer.BusinessObject.DataToPersist
{
    [Serializable]
    public class LocalConfiguration
    {
        public string SourceDirectory { get; set; }
        public string DestinationDirectory { get; set; }
        public OverridableData[] Artists { get { return ArtistsEntireList.Where(item => string.IsNullOrEmpty(item.OverridenName)).ToArray(); }}
        public OverridableData[] ArtistsEntireList { get; set; }
        public OverridableData[] Albums { get { return AlbumsEntireList.Where(item => string.IsNullOrEmpty(item.OverridenName)).ToArray(); } }
        public OverridableData[] AlbumsEntireList { get; set; }
        public bool UseITunesLibrary { get; set; }


        public LocalConfiguration()
        {
            ArtistsEntireList = new OverridableData[0];
            AlbumsEntireList = new OverridableData[0]; 
        }
        public void Save(string file)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(LocalConfiguration));
            StreamWriter writer = new StreamWriter(file);
            serializer.Serialize(writer, this);
            writer.Close();
        }

        public void Load(string file)
        {
            
            XmlSerializer serializer = new XmlSerializer(typeof(LocalConfiguration));
            if (!System.IO.File.Exists(file))
            {               
                var stream = System.IO.File.Create(file);
                stream.Close();
                Save(file);
            }
            FileStream fs = new FileStream(file, FileMode.Open);
            LocalConfiguration lc;
            lc = (LocalConfiguration)serializer.Deserialize(fs);
            this.AlbumsEntireList = lc.AlbumsEntireList;
            this.ArtistsEntireList = lc.ArtistsEntireList;
            this.DestinationDirectory = lc.DestinationDirectory;
            this.SourceDirectory = lc.SourceDirectory;
            this.UseITunesLibrary = lc.UseITunesLibrary;
        }
    }
}
